package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gec.model.entity.SysActivity;
import com.gec.model.vo.SysActivityQueryVo;
import com.gec.system.mapper.SysActivityMapper;
import com.gec.system.service.SysActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author Liangxj
 * @since 2023-12-09
 */
@Service
public class SysActivityServiceImpl extends ServiceImpl<SysActivityMapper, SysActivity> implements SysActivityService {

    @Override
    public IPage<SysActivity> selectPage(IPage<SysActivity> page1, SysActivityQueryVo vo) {
        return this.getBaseMapper().selectPage(page1, vo);
    }

    @Override
    public List<SysActivity> findAll() {
        QueryWrapper<SysActivity> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("status");
        List<SysActivity> sysActivities = this.getBaseMapper().selectList(wrapper);

        Date now = new Date();

        //重新设置所有活动状态
        sysActivities.forEach(ac->{
            Date startTime = ac.getStartTime();
            Date endTime = ac.getEndTime();
            Integer preStatus = ac.getStatus();
            if (now.before(startTime)){
                //准备中
                ac.setStatus(0);
            }
            else if (startTime.before(now)&&endTime.after(now)){
                ac.setStatus(1);
            }else if (endTime.before(now)){
                //结束
                ac.setStatus(2);
            }
            if (preStatus!=ac.getStatus()){
                this.getBaseMapper().updateById(ac);
            }


        });
        List<SysActivity> sysActivities2 = this.getBaseMapper().selectList(wrapper);
        return sysActivities2;
    }
}
